//
//  GaoJinViewController.m
//  Runone
//
//  Created by runone01 on 16/11/21.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "GaoJinViewController.h"
#import "RequestHelper.h"
#import "SVProgressHUD.h"
#import "MJExtension.h"
#import "GaoJinModel.h"
#import "GaoJinCell.h"
#import "GaoJingDetailViewController.h"
//#import "UIColor+ZXLazy.h"
#import "VehicleGaoJinDetailController.h"

@interface GaoJinViewController ()<UIAlertViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation GaoJinViewController

#pragma     ----------告警信息界面



- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"告警信息";
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    if (_type == 1) {
       label.text=@"两客一危告警";
    }else {
    label.text=@"车辆管理告警";
    }
    
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self request];

    _dataSource=[[NSMutableArray alloc] init];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"GaoJinCell" bundle:nil] forCellReuseIdentifier:@"GaoJinCell"];

}

-(void)viewWillAppear:(BOOL)animated  {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden=NO;
}

//加载数据
- (void)request {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAllLastestVehicleDynamicModel=[[RequestGinsengModel alloc] init];
    GetAllLastestVehicleDynamicModel.URL=GetAllLastestVehicleDynamicURL;
    GetAllLastestVehicleDynamicModel.DataValue=[self DataValueEventUId:self.type pageString:10 andSizeString:@""];
    GetAllLastestVehicleDynamicModel.UpdatedTime=[requestHelper getCurrentTime];
    GetAllLastestVehicleDynamicModel.SystemCode=@"GuangleHighway";
    GetAllLastestVehicleDynamicModel.MethodName=@"GetAMAlarmInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel) {
        NSArray *arr=[requestOutginseng.DataValue mj_JSONObject];
        for (NSDictionary *dic in arr) {
            GaoJinModel *model=[GaoJinModel mj_objectWithKeyValues:dic];
    
            [_dataSource addObject:model];
        }
        if (!_dataSource.count) {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无数据！" delegate:self cancelButtonTitle:@"确 定" otherButtonTitles:nil, nil];
            [alert show];
            return ;
        }
        else
        {
        [self.tableView reloadData];
        }
        NSLog(@"%lu",(unsigned long)arr.count);
    };
    [requestHelper RequestGinsengModel:GetAllLastestVehicleDynamicModel andPushController:self];
}

-(NSString *)DataValueEventUId:(int)eventUID pageString:(int)pageStr andSizeString:(NSString *)sizeStr {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"VehicleType\",\"value\":\"%ld\"},{\"key\":\"PageSize\",\"value\":\"%ld\"},{\"key\":\"VehicleAlarmInfoUID\",\"value\":\"%@\"}]",(long)eventUID,(long)pageStr,sizeStr];
    return valueStr;
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GaoJinCell *cell   = [tableView dequeueReusableCellWithIdentifier:@"GaoJinCell" forIndexPath:indexPath];
    GaoJinModel *model = _dataSource[indexPath.row];
    [cell setCellForModel:model];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VehicleGaoJinDetailController *detailVC = [[VehicleGaoJinDetailController alloc] init];
    detailVC.gaoModel                       = _dataSource[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}



@end
